#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "adc.h"

extern char uart_cmd[256];
extern uint32_t uartIndex;

void CMD_ParseOptions(char **options, int size)
{
  for (int i = 0; i < size; i++)
  {
    char *cmd[2];
    char *tmpstr = strtok(options[i], "=");
    int ct = 0;
    while (tmpstr != NULL)
    {
      cmd[ct++] = tmpstr;
      if (ct >= 2)
      {
        break;
      }
      tmpstr = strtok(NULL, " ");
    }
    if (ct == 2)
    {
      printf("cmd=%s value=%s\n", cmd[0], cmd[1]);
			if(!strcmp(cmd[0],"tp")) {
				int t = atoi(cmd[1]);
				if(t>=10 && t<=100) {
					g_adc_ctrl.shortPluse = t;
				}
			}
			else if(!strcmp(cmd[0],"adh")) {
				int t = atoi(cmd[1]);
				if(t>=1500 && t<=4000) {
					g_adc_ctrl.adcHigh = t;
				}
			}
			else if(!strcmp(cmd[0],"adl")) {
				int t = atoi(cmd[1]);
				if(t>=500 && t<=1500) {
					g_adc_ctrl.adcLow = t;
				}
			}
			else if(!strcmp(cmd[0],"ch")) {
				int t = atoi(cmd[1]);
				if(t>=0 && t<=1) {
					g_adc_ctrl.channel = t;
				}
			}
			else if(!strcmp(cmd[0],"nplc")) {
				int t = atoi(cmd[1]);
				if(t>=1 && t<=150) {
					g_adc_ctrl.nplc = t;
					g_adc_ctrl.nplcTimeCT = t*400;
					g_adc_ctrl.zeroN = 0;
					g_adc_ctrl.zeroP = 0;
				}
			}
			else if(!strcmp(cmd[0],"zeroP")) {
				double t = strtod(cmd[1],NULL);
				if(t>=0 && t<=200000000) {
					g_adc_ctrl.zeroP = t;
				}
			}
			else if(!strcmp(cmd[0],"zeroN")) {
				double t = strtod(cmd[1],NULL);
				if(t>=0 && t<=200000000) {
					g_adc_ctrl.zeroN = t;
				}
			}
			else if(!strcmp(cmd[0],"ratio")) {
				double t = strtod(cmd[1],NULL);
				if(t>=1 && t<=1.2) {
					g_adc_ctrl.refRatio = t;
				}
			}
			else if(!strcmp(cmd[0],"refct")) {
				double t = strtod(cmd[1],NULL);
				if(t>=1 && t<=15000000) {
					g_adc_ctrl.refCT = t;
					g_adc_ctrl.update = 1;
				}
			}
			else if(!strcmp(cmd[0],"az")) {
				int t = atoi(cmd[1]);
				if(t>=0) {
					g_adc_ctrl.az = t;
					if(g_adc_ctrl.az) {
						g_adc_status.isZero = 1;
					}
					else {
						g_adc_status.isZero = 0;
					}
				}
			}
			else if(!strcmp(cmd[0],"dt")) {
				int t = atoi(cmd[1]);
				if(t>=0) {
					g_adc_ctrl.displayType = t;
				}
			}
			else if(!strcmp(cmd[0],"type")) {
				int t = atoi(cmd[1]);
				if(t>=0) {
					g_adc_ctrl.type = t;
				}
			}
    }
  }
}

void CMD_Parse(void)
{
  int end = 0;
  int start = 0;
  int i = start;
  //寻找命令起始位置
  for (; i < uartIndex; i++)
  {
    if ((uart_cmd[i] != '\r') && (uart_cmd[i] != '\n'))
    {
      start = i;
      break;
    }
  }
	//寻找结束位置
  for (; i < uartIndex; i++)
  {
    if ((uart_cmd[i] == '\r') || (uart_cmd[i] == '\n'))
    {
      end = i;
      break;
    }
  }
  if (start < end)
  {
    uart_cmd[end] = 0;
    char *options[20];
    int ct_opt = 0;
		//分割字符串
    char *tmpstr = strtok(uart_cmd + start, " ");
    while (tmpstr != NULL)
    {
      options[ct_opt++] = tmpstr;
      if (ct_opt >= 20)
      {
        break;
      }
      tmpstr = strtok(NULL, " ");
    }
		//处理配置数组
    CMD_ParseOptions(options, ct_opt);
  }
	//将处理后的缓存清空
  start = end + 1;
  for (i = start; i < uartIndex; i++)
  {
    if ((uart_cmd[i] != '\r') && (uart_cmd[i] != '\n'))
    {
      break;
    }
  }
  start = 0;
  char haveNewLine = 0;
  for (; i < uartIndex; i++)
  {
    uart_cmd[start++] = uart_cmd[i];
    if ((uart_cmd[i] == '\r') || (uart_cmd[i] == '\n'))
    {
      haveNewLine = 1;
    }
  }
  uart_cmd[start] = 0;
  uartIndex = start;
	//如果还有完整的命令行,递归调用
  if (haveNewLine)
  {
    CMD_Parse();
  }
}
